#!/bin/bash
# ESP32-RK3588传感器控制系统 - 环境配置脚本

echo "=== ESP32-RK3588传感器控制系统 - 环境配置 ==="

# 检查Python版本
python3 --version
if [ $? -ne 0 ]; then
    echo "错误: 未找到Python3，请先安装Python 3.8+"
    exit 1
fi

# 创建虚拟环境
echo "创建Python虚拟环境..."
python3 -m venv esp_venv
if [ $? -ne 0 ]; then
    echo "错误: 创建虚拟环境失败"
    exit 1
fi

# 激活虚拟环境
echo "激活虚拟环境..."
source esp_venv/bin/activate

# 安装依赖
echo "安装Python依赖..."
pip install --upgrade pip
pip install -r ../rk3588_controller/requirements.txt

# 检查esptool
echo "检查esptool..."
esptool.py version
if [ $? -ne 0 ]; then
    echo "警告: esptool安装可能有问题"
fi

# 检查platformio
echo "检查PlatformIO..."
pio --version
if [ $? -ne 0 ]; then
    echo "警告: PlatformIO安装可能有问题"
fi

# 创建数据目录
echo "创建数据目录..."
mkdir -p ../rk3588_controller/data

echo "环境配置完成!"
echo "使用方法:"
echo "  1. 激活虚拟环境: source esp_venv/bin/activate"
echo "  2. 编译ESP32固件: cd ../esp32_firmware && pio run"
echo "  3. 刷写固件: python3 flash_esp32.py --firmware ../esp32_firmware/.pio/build/esp32dev/firmware.bin"
echo "  4. 运行控制程序: cd ../rk3588_controller && python3 main.py"